// 本文件对Nack的相关功能进行性能测试
// @Author: zhengqi Wu
// @Description:
// @Version: 1.0.0
// @Date: 2021/4/16 13:11 下午
// @Copyright: MIN-Group；国家重大科技基础设施——未来网络北大实验室；深圳市信息论与未来网络重点实验室
//

const benchmark = require('benchmark')
const {createPayLoad} = require("../common/createPayload");
const {createBlockByBuffer} = require("../encoding/Block");
const {createIdentifierByString} = require("../component/Identifier");
const {Nack} = require("./Nack");
const {Encoder, MaxPacketSize} = require("../encoding/Encoder")
const payload = createPayLoad(0), payload1K = createPayLoad(1), payload8K = createPayLoad(8)

function createNack(payload) {
    let nack = new Nack()
    nack.setNackReason(1)
    let name = createIdentifierByString('/interest')
    nack.Interest.setName(name)
    nack.Interest.Payload.setValue(payload)
    return nack
}

let nack = createNack(payload), nack1K = createNack(payload1K), nack8K = createNack(payload8K)
const suite1 = new benchmark.Suite()
suite1.add('wireEncode 1Byte', ()=>{
    let enc = new Encoder()
    enc.encoderReset(MaxPacketSize, 0)
    nack.wireEncode(enc)
}).add('wireEncode 1K', ()=>{
    let enc = new Encoder()
    enc.encoderReset(MaxPacketSize, 0)
    nack1K.wireEncode(enc)
}).add('wireEncode 8K', ()=>{
    let enc = new Encoder()
    enc.encoderReset(MaxPacketSize, 0)
    nack8K.wireEncode(enc)
}).on('cycle', (event) => {
    console.log(String(event.target))
}).on('complete', () =>{
    console.log('wireEncode finished')
}).run()

let enc = new Encoder()
enc.encoderReset(MaxPacketSize, 0)
nack.wireEncode(enc)
let buf = enc.getBuffer()
let nack2 = new Nack()
let block = createBlockByBuffer(buf, false)
// let enc = new Encoder()
enc.encoderReset(MaxPacketSize, 0)
nack1K.wireEncode(enc)
buf = enc.getBuffer()
let nack2_1K = new Nack()
let block1K = createBlockByBuffer(buf, false)
// let enc = new Encoder()
enc.encoderReset(MaxPacketSize, 0)
nack8K.wireEncode(enc)
buf = enc.getBuffer()
let nack2_8K = new Nack()
let block8K = createBlockByBuffer(buf, false)
const suite2 = new benchmark.Suite()
suite2.add('wireDecode 1Byte', ()=>{
    nack2.wireDecode(block)
}).add('wireDecode 1K', ()=>{
    nack2_1K.wireDecode(block1K)
}).add('wireDecode 8K', ()=>{
    nack2_8K.wireDecode(block8K)
}).on('cycle', (event) => {
    console.log(String(event.target))
}).on('complete', () =>{
    console.log('wireDecode finished')
}).run()

